/**
 * Copyright (c) 2010, Casey Lai
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 * 
 * Neither the name of the Cheetah Group nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

package org.cheetahworkflow.designer.utils;

import java.util.Collections;
import java.util.List;

import org.cheetahworkflow.designer.model.Arrow;
import org.cheetahworkflow.designer.model.Chart;
import org.cheetahworkflow.designer.model.Comment;
import org.cheetahworkflow.designer.model.ConditionArrow;
import org.cheetahworkflow.designer.model.EndNode;
import org.cheetahworkflow.designer.model.ModuleNode;
import org.cheetahworkflow.designer.model.Node;
import org.cheetahworkflow.designer.model.SequenceNode;
import org.cheetahworkflow.designer.model.StartNode;
import org.cheetahworkflow.designer.model.SwitchNode;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;

public class XMLSerializer {
	
	public static final String TYPE_SEQUENCE = "sequence";
	
	public static final String TYPE_SWITCH = "switch";
	
	public static final String TYPE_MODULE = "module";

	public static String serialize(Chart chart) {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
		sb.append("<workflow xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n\txsi:noNamespaceSchemaLocation=\"workflow.xsd\">\n\n");
		
		StartNode startNode = chart.getStartNode();
		if (startNode != null) {
			sb.append(XMLSerializer.serialize(startNode));
		}
		
		List<Node> sortedNodes = chart.getNodes();
		Collections.sort(sortedNodes, NodeSortor.getInstance());
		for(Node node: sortedNodes) {
			if (node instanceof SequenceNode) {
				sb.append(XMLSerializer.serialize((SequenceNode) node));
			} else if (node instanceof SwitchNode) {
				sb.append(XMLSerializer.serialize((SwitchNode) node));
			} else if (node instanceof ModuleNode) {
				sb.append(XMLSerializer.serialize((ModuleNode) node));
			} else if (node instanceof EndNode) {
				sb.append(XMLSerializer.serialize((EndNode) node));
			} else if (node instanceof Comment) {
				sb.append(XMLSerializer.serialize((Comment) node));
			}
		}
		sb.append("</workflow>\n");
		return sb.toString();
	}
	
	public static String serialize(StartNode node) {
		Point location = node.getLocation();
		List<Arrow> outgoings = node.getOutgoingArrow();
		String targetName = "";
		if (outgoings.size() > 0) {
			targetName = outgoings.get(0).getTarget().getName();
		}
		return "\t<start location=\"" + location.x + "," + location.y + "\">" + targetName + "</start>\n\n";
	}
	
	public static String serialize(SequenceNode node) {
		StringBuilder sb = new StringBuilder();
		Point location = node.getLocation();
		Dimension size = node.getSize();
		sb.append("\t<node type=\"" + TYPE_SEQUENCE + "\" name=\"" + node.getName() + "\" location=\"" + location.x + "," + location.y + "\" size=\"" + size.width + "," + size.height + "\">\n");
		sb.append("\t\t<caption><![CDATA[" + node.getCaption() + "]]></caption>\n");
		sb.append("\t\t<description><![CDATA[" + node.getDescription() + "]]></description>\n");
		
		String parameter = node.getParameter();
		if (parameter.isEmpty()) {
			sb.append("\t\t<class name=\"" + node.getClassName() + "\"></class>\n");
		} else {
			sb.append("\t\t<class name=\"" + node.getClassName() + "\">\n");
			sb.append("\t\t\t<parameter>" + parameter + "</parameter>\n");
			sb.append("\t\t</class>\n");
		}
		
		List<Arrow> outgoings = node.getOutgoingArrow();
		String targetName = "";
		if (outgoings.size() > 0) {
			targetName = outgoings.get(0).getTarget().getName();
		}
		sb.append("\t\t<next-node>" + targetName + "</next-node>\n");
		sb.append("\t</node>\n\n");
		
		return sb.toString();
	}
	
	public static String serialize(SwitchNode node) {
		StringBuilder sb = new StringBuilder();
		Point location = node.getLocation();
		Dimension size = node.getSize();
		sb.append("\t<node type=\"" + TYPE_SWITCH + "\" name=\"" + node.getName() + "\" location=\"" + location.x + "," + location.y + "\" size=\"" + size.width + "," + size.height + "\">\n");
		sb.append("\t\t<caption><![CDATA[" + node.getCaption() + "]]></caption>\n");
		sb.append("\t\t<description><![CDATA[" + node.getDescription() + "]]></description>\n");
		
		ConditionArrow defaultArrow = null;
		for(Arrow arrow: node.getOutgoingArrow()) {
			ConditionArrow conditionArrow = (ConditionArrow) arrow;
			if (conditionArrow.isDefault()) {
				defaultArrow = conditionArrow;
				continue;
			}
			sb.append("\t\t<switch>\n");
			String parameter = conditionArrow.getParameter();
			if (parameter.isEmpty()) {
				sb.append("\t\t\t<condition class=\"" + conditionArrow.getClassName() + "\"></condition>\n");
			} else {
				sb.append("\t\t\t<condition class=\"" + conditionArrow.getClassName() + "\">\n");
				sb.append("\t\t\t\t<parameter>" + parameter + "</parameter>\n");
				sb.append("\t\t\t</condition>\n");
			}
			sb.append("\t\t\t<next-node>" + conditionArrow.getTarget().getName() + "</next-node>\n");
			sb.append("\t\t</switch>\n");
		}
		
		if (defaultArrow != null) {
			sb.append("\t\t<switch>\n");
			sb.append("\t\t\t<default-node>" + defaultArrow.getTarget().getName() + "</default-node>\n");
			sb.append("\t\t</switch>\n");
		}
		
		sb.append("\t</node>\n\n");
		
		return sb.toString();
	}
	
	public static String serialize(ModuleNode node) {
		StringBuilder sb = new StringBuilder();
		Point location = node.getLocation();
		Dimension size = node.getSize();
		sb.append("\t<node type=\"" + TYPE_MODULE + "\" name=\"" + node.getName() + "\" location=\"" + location.x + "," + location.y + "\" size=\"" + size.width + "," + size.height + "\">\n");
		sb.append("\t\t<caption><![CDATA[" + node.getCaption() + "]]></caption>\n");
		sb.append("\t\t<description><![CDATA[" + node.getDescription() + "]]></description>\n");
		sb.append("\t\t<ref><![CDATA[" + node.getRef() + "]]></ref>\n");
		sb.append("\t\t<ref-classpath><![CDATA[" + node.getClasspath() + "]]></ref-classpath>\n");
		
		List<Arrow> outgoings = node.getOutgoingArrow();
		String targetName = "";
		if (outgoings.size() > 0) {
			targetName = outgoings.get(0).getTarget().getName();
		}
		sb.append("\t\t<next-node>" + targetName + "</next-node>\n");
		sb.append("\t</node>\n\n");
		
		return sb.toString();
	}
	
	public static String serialize(EndNode node) {
		Point location = node.getLocation();
		return "\t<end name=\"" + node.getName() + "\" location=\"" + location.x + "," + location.y + "\"></end>\n\n";
	}
	
	public static String serialize(Comment comment) {
		Point location = comment.getLocation();
		Dimension size = comment.getSize();
		return "\t<comment location=\"" + location.x + "," + location.y + "\" size=\"" + size.width + "," + size.height + "\"><![CDATA[" + comment.getCaption() + "]]></comment>\n\n";
	}
}
